/*
 * @Date: 2022-02-14 11:24:10
 * @LastEditors: 春贰
 * @gitee: https://gitee.com/chun22222222
 * @github: https://github.com/chun222
 * @Desc:
 * @LastEditTime: 2022-03-25 17:48:18
 * @FilePath: \server\system\controller\base\login.go
 */
package base

import (
	"go2admin_pure/system/core/request"
	"go2admin_pure/system/core/response"
	"go2admin_pure/system/model/DbModel"
	"go2admin_pure/system/model/RequestModel"
	"go2admin_pure/system/service/BaseService"
	"go2admin_pure/system/util/jwtUtil"
	"github.com/gin-gonic/gin"
)

var loginService BaseService.LoginService

/**
*
* @api {post} /base/login 登录
* @apiName 登录接口
* @apiGroup 基本
* @apiVersion  1.0.0
*
* @apiBody  {String} loginname 登录名
* @apiBody  {String} password 密码
* @apiSampleRequest /base/login
* @apiSuccess {String} powers 权限.
*
* @apiParamExample  {json} Request-Example:
* {
*     loginname : "value",
	  password : "password",
* }
*
*
* @apiSuccessExample {json} Success-Response:
*
{
    "code": 0,
    "data": {
        "id": 2,
        "create_time": "0001-01-01T00:00:00Z",
        "update_time": "0001-01-01T00:00:00Z",
        "login_name": "aaa",
        "user_name": "aaa",
        "status": 0,
        "email": "",
        "phone": "",
        "token": " ",
        "powers": []
    },
    "msg": "sucess"
}
*
*
*/
func Login(c *gin.Context) {
	var r RequestModel.Login
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	err, u := loginService.Login(&DbModel.SysUser{LoginName: r.Loginname, Password: r.Password, Kick: r.Kick})
	if err != nil {
		response.FailWithMessage("error password or loginname", c)
		return
	}

	//创建token
	j := jwtUtil.NewJWT()
	claims := j.CreateClaims(jwtUtil.BaseClaims{
		ID:        u.ID,
		LoginName: u.LoginName,
		UserName:  u.UserName,
	})
	token, _ := j.CreateToken(claims)
	u.Token = token
	response.OkWithDetailed(u, "sucess", c)

}

/**
 *
 * @api {post} path title
 * @apiName apiName
 * @apiGroup apiGroup
 * @apiVersion  1.0.0
 *
 * @apiBody  {String}  apiBody
 * @apiSampleRequest 请求地址
 * @apiSuccess {String} filed 描述
 *
 * @apiParamExample  {json} Request-Example:
 * {
 *    key : "value",
 * }
 *
 *
 * @apiSuccessExample {json} Success-Response:
 *
 {
  "code": 0,
	 "data": {
	 },
	 "msg": "sucess"
 }
 *
 *
*/
func Login2(c *gin.Context) {
	j := jwtUtil.NewJWT()
	x_token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVVUlEIjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIiwiSUQiOjEsIlVzZXJuYW1lIjoidXNlci5Vc2VybmFtZSIsIk5pY2tOYW1lIjoidXNlci5OaWNrTmFtZSIsIkF1dGhvcml0eUlkIjoiMTExMTExIiwiQnVmZmVyVGltZSI6MSwiZXhwIjoxNjQ0OTg5MDMzLCJpc3MiOiJzdHJpbmciLCJuYmYiOjE2NDQ5ODgwMjd9.geFlg9sU1geih6xBDImINwLeUcICNkqaxebpIHHAJUA"
	t := c.DefaultQuery("t", x_token)
	r, e := j.ParseToken(t)
	if r != nil {
		response.OkWithData(r, c)
	} else {
		response.FailWithMessage(e.Error(), c)
	}

}
